package cn.edu.hit.hec;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

@RestController
@RequestMapping("/")
public class Controller {
    @Autowired
    HttpServletRequest request;

    //    @CrossOrigin
//    @RequestMapping(method = RequestMethod.GET, value = "/",produces="text/html;charset=UTF-8")
//    public String isRun(){
//        return "<!DOCTYPE html>\n" +
//                "<html>\n" +
//                "<head>\n" +
//                "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n" +
//                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0\" />\n" +
//                "    <title>发电机组远程故障诊断系统</title>\n" +
//                "</head>\n" +
//                "<body>微服务测试页</body>\n" +
//                "</html>";
//    }
    @CrossOrigin
    @RequestMapping(method = RequestMethod.POST, value = "query2", produces = "application/json;charset=UTF-8", consumes = "application/json")
    public String startinfer() {

//        SpringApplication.run(Application.class);

        return "aaa";
    }


    @CrossOrigin
    @RequestMapping(method = RequestMethod.POST, value = "start", produces = "application/json;charset=UTF-8", consumes = "application/json")
    public String savetofile() throws IOException {

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
        String path = "./data.json";
        File file = new File(path);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            fileOutputStream.write(line.getBytes());
        }
        fileOutputStream.close();

        App2.main(null);

        return "推理完成";
    }
}
